var vm = new Vue({
    el:"#app",
    data(){
        return {
            //搜索条件
            ProductNameOrNo:'',
            formItem: {
                ProductNo: '',
                Brand:'',
                Name:'',
                Specification:'',
                Type:'',
                BarCode:'',
                Unit:'',
                Package:'',
                Remark:''
            },
            EditFormItem:{
                ProductNo: '',
                Brand:'',
                Name:'',
                Specification:'',
                Type:'',
                BarCode:'',
                Unit:'',
                Package:'',
                Remark:''
            },
            modal1:false, //添加产品弹框
            modal2:false, //编辑产品弹框
            //搜索结果
            columns1: [
                {
                    type: 'index',
                    title: '序号',
                    width:46,
                    align: 'center',
                },
                {
                    title: '产品编号',
                    key: 'ProductNo',
                    align: 'center',
                },
                {
                    title: '产品名称',
                    key: 'Name',
                    align: 'center',
                    width:300
                },
                {
                    title: '产品品牌',
                    key: 'Brand',
                    align: 'center',
                },
                {
                    title: '产品规格',
                    key: 'Specification',
                    align: 'center',

                },
                {
                    title: '产品型号',
                    key: 'Type',
                    align: 'center',
                },
                {
                    title: '产品单位',
                    key: 'Unit',
                    align: 'center',
                },
                {
                    title: '包装规格',
                    key: 'Package',
                    align: 'center',
                },
                {
                    title: '商品条码',
                    key: 'BarCode',
                    align: 'center',
                },
                {
                    title: '备注',
                    key: 'Remark',
                    align: 'center',
                },
                {   title: '操作',
                    key: 'action',
                    align: 'center',
                    width:150,
                    render: (h, params) => {
                        return h('div', [
                            h('a', {
                                style: {
                                    marginRight: '5px',
                                    border : '0',
                                    fontSize: '14px',
                                },
                                on: {
                                    click: () => {
                                        this.EditFormItem.ProductNo = params.row.ProductNo;
                                        this.EditFormItem.Brand = params.row.Brand;
                                        this.EditFormItem.Name = params.row.Name;
                                        this.EditFormItem.Specification = params.row.Specification;
                                        this.EditFormItem.Type = params.row.Type;
                                        this.EditFormItem.BarCode = params.row.BarCode;
                                        this.EditFormItem.Unit = params.row.Unit;
                                        this.EditFormItem.Package = params.row.Package;
                                        this.EditFormItem.Remark = params.row.Remark;
                                        this.modal2=true;
                                    }
                                }
                            },'编辑'),
                            h('span', {
                            },'|'),
                            h('a', {
                                style: {
                                    marginLeft: '5px',
                                    border : '0',
                                    fontSize: '14px',
                                },
                                on: {
                                    click: () => {
                                        var ProductNo = params.row.ProductNo;
                                        this.confirm(ProductNo);
                                    }
                                }
                            },'删除')
                        ])
                    }
                }
            ],
            data1: [],

        }
    },
    created(){
        this.initThisPage();
    },
    methods:{
        ok () {
        },
        cancel () {
        },
        confirm (ProductNo) {
            var self = this;
            this.$Modal.confirm({
                title: '提示',
                content: '<p>是否删除</p>',
                onOk: () => {
                    self.delProduct(ProductNo);
                },
                onCancel: () => {
                    toastr.info("取消删除");
                }
            });
        },
        //打开添加经销商弹框
        openModel1(){
            this.formItem.ProductNo = "CP-"+this.uuid(8, 16);
            this.modal1 = true;
        },
        querytable(){
            var self = this;
            var info ;
            var ProductNameOrNo = self.ProductNameOrNo;
            var jsondata={"ProductNameOrNo":ProductNameOrNo};
            var data_json = JSON.stringify(jsondata);
            $.ajax({
                url: "./goProduct/queryAllProduct", //请求的url地址
                dataType: "json", //返回格式为json
                data: data_json, //参数值
                async:false,
                type: "POST", //请求方式
                processData: false, //对表单data数据是否进行序列化
                contentType: false, //dataType设置你收到服务器数据的格式
                headers: {
                    'Content-Type': 'application/json'
                },
                success: function(data) {
                    info = eval(data.ResultList);
                },
                error: function() {},
            });
            if(info){
                self.data1 = info;
            }
        },
        saveProduct(){
            var self = this;
            var ProductNo = self.formItem.ProductNo;
            var Name = self.formItem.Name;
            Name = Name.trim();
            if(Name==null||Name==""){
                toastr.warning("请录入产品名称");
                return false;
            }
            var Brand = self.formItem.Brand;
            var Specification = self.formItem.Specification;
            var Type = self.formItem.Type;
            var Unit = self.formItem.Unit;
            var Package = self.formItem.Package;
            var BarCode = self.formItem.BarCode;
            var Remark = self.formItem.Remark;
            var jsondata = {
                "ProductNo":ProductNo,
                "Name":Name,
                "Brand":Brand,
                "Specification":Specification,
                "Type":Type,
                "Unit":Unit,
                "Package":Package,
                "BarCode":BarCode,
                "Remark":Remark
            }
            var data_json = JSON.stringify(jsondata);
            $.ajax({
                url: "./goProduct/saveProduct", //请求的url地址
                xhrFields: {
                    withCredentials: true
                },
                dataType: "json", //返回格式为json
                data: data_json, //参数值
                type: "POST", //请求方式
                headers : {
                    'Content-Type' : 'application/json'
                },
                beforeSend: function() {
                    //请求前的处理
                },
                success: function(data) {
                    if(data.Status=='1'){
                        toastr.success('保存成功');
                        self.formItem.ProductNo = "";
                        self.formItem.Name = "";
                        self.formItem.Brand = "";
                        self.formItem.Specification = "";
                        self.formItem.Type = "";
                        self.formItem.Unit = "";
                        self.formItem.Package = "";
                        self.formItem.BarCode = "";
                        self.formItem.Remark = "";
                        self.querytable();
                    }else{
                        toastr.error('保存失败，请重新录入');
                    }
                },
                complete: function() {
                    //请求完成的处理
                },
                error: function(data) {

                }
            });
        },
        // 编辑弹框
        editProduct(){
            var self = this;
            var ProductNo = self.EditFormItem.ProductNo;
            var Name = self.EditFormItem.Name;
            Name = Name.trim();
            if(Name==null||Name==""){
                toastr.warning("请录入产品名称");
                return false;
            }
            var Brand = self.EditFormItem.Brand;
            var Specification = self.EditFormItem.Specification;
            var Type = self.EditFormItem.Type;
            var Unit = self.EditFormItem.Unit;
            var Package = self.EditFormItem.Package;
            var BarCode = self.EditFormItem.BarCode;
            var Remark = self.EditFormItem.Remark;
            var jsondata = {
                "ProductNo":ProductNo,
                "Name":Name,
                "Brand":Brand,
                "Specification":Specification,
                "Type":Type,
                "Unit":Unit,
                "Package":Package,
                "BarCode":BarCode,
                "Remark":Remark
            }
            var data_json = JSON.stringify(jsondata);
            $.ajax({
                url: "./goProduct/editProduct", //请求的url地址
                xhrFields: {
                    withCredentials: true
                },
                dataType: "json", //返回格式为json
                data: data_json, //参数值
                type: "POST", //请求方式
                headers : {
                    'Content-Type' : 'application/json'
                },
                beforeSend: function() {
                    //请求前的处理
                },
                success: function(data) {
                    if(data.Status=='1'){
                        toastr.success('编辑成功');
                        self.querytable();
                    }else{
                        toastr.error('编辑失败，请重新编辑');
                    }
                },
                complete: function() {
                    //请求完成的处理
                },
                error: function(data) {

                }
            });
        },
        delProduct(ProductNo){
            var self = this;
            var jsondata = {
                "ProductNo":ProductNo,
            }
            var data_json = JSON.stringify(jsondata);
            $.ajax({
                url: "./goProduct/delProduct", //请求的url地址
                xhrFields: {
                    withCredentials: true
                },
                dataType: "json", //返回格式为json
                data: data_json, //参数值
                type: "POST", //请求方式
                headers : {
                    'Content-Type' : 'application/json'
                },
                beforeSend: function() {
                    //请求前的处理
                },
                success: function(data) {
                    if(data.Status=='1'){
                        toastr.success("删除成功");
                        self.querytable();
                    }else{
                        toastr.error('删除失败，请刷新页面后重试');
                    }
                },
                complete: function() {
                    //请求完成的处理
                },
                error: function(data) {

                }
            });
        },
        initThisPage(){
            var self = this;
            $.ajax({
                url: "./goLogin/initIndexPage", //请求的url地址
                xhrFields: {
                    withCredentials: true
                },
                async:false,
                dataType: "json", //返回格式为json
                type: "GET", //请求方式
                processData: false, //对表单data数据是否进行序列化
                contentType: false, //dataType设置你收到服务器数据的格式
                headers: {
                    'Content-Type': 'application/json'
                },
                beforeSend: function() {
                    //请求前的处理
                },
                success: function(data) {
                    if(data.Status==0){
                        // top.location.href = "../../login.html";
                        window.location.href="../../login.html";
                    }
                },
                complete: function() {
                    //请求完成的处理
                },
                error: function() {}
            });
        },

        //手机号验证
        isPoneAvailable(str) {
            var myreg=/^[1][3,4,5,7,8][0-9]{9}$/;
            if (!myreg.test(str)) {
                return false;
            } else {
                return true;
            }
        },
        //uuid
        uuid(len, radix) {
            var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
            var uuid = [], i;
            radix = radix || chars.length;

            if (len) {
                // Compact form
                for (i = 0; i < len; i++) uuid[i] = chars[0 | Math.random()*radix];
            } else {
                // rfc4122, version 4 form
                var r;

                // rfc4122 requires these characters
                uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
                uuid[14] = '4';

                // Fill in random data. At i==19 set the high bits of clock sequence as
                // per rfc4122, sec. 4.1.5
                for (i = 0; i < 36; i++) {
                    if (!uuid[i]) {
                        r = 0 | Math.random()*16;
                        uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
                    }
                }
            }

            return uuid.join('');
        }
    }
})